package com.atguigu.srb.oss.Controller.Api;

import com.atguigu.common.result.R;
import com.atguigu.srb.oss.Service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @program: srb
 * @author: 龙龙
 * @create: 2022-02-11 10:08
 * @description:
 **/
//@CrossOrigin
@Api(tags = "阿里云文件管理")
@RestController
@RequestMapping("/api/oss/file")
public class FileController {

    @Autowired
    private FileService fileService;


    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public R upload(
            @ApiParam(value = "文件", required = true)
            @RequestParam("file")MultipartFile multipartFile,

            @ApiParam(value = "模块", required = true)
            @RequestParam("module")String module){
       String url = fileService.upload(multipartFile,module);

        return R.ok().message("文件上传成功").data("url",url);
    }

    @ApiOperation("删除OSS文件")
    @DeleteMapping("/remove")
    public R remove(
            @ApiParam(value = "要删除的文件路径", required = true)
            @RequestParam("url") String url) {
        fileService.removeFile(url);  //URL = 云服务地址 + 云服务存储地址（删除的API参数）
        return R.ok().message("删除成功");
    }

}
